package com.bangdream.michelia.view.fragment.main.my;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.ExamContract;
import com.bangdream.michelia.entity.exam.ExamBean;
import com.bangdream.michelia.presenter.ExamPresenter;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.utils.PublicResources;
import com.bangdream.michelia.view.activity.exam.ExamActivity;
import com.bangdream.michelia.view.activity.exam.ExamExplainActivity;
import com.bangdream.michelia.view.activity.exam.ExamResultActivity;
import com.bangdream.michelia.view.activity.my.MyExam;
import com.bangdream.michelia.view.adapter.MyExamAdapter;
import com.bangdream.michelia.view.adapter.MyExamListAdapter;
import com.bangdream.michelia.view.adapter.TaskQrderAdapter;
import com.bangdream.michelia.view.fragment.currency.BaseFragment;
import com.bangdream.michelia.view.fragment.currency.LazyLoadFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date on 2018/8/11 11:57
 * @email 909732184@qq.com
 */
public class MyExamFragment extends LazyLoadFragment<ExamContract.IMyExam, ExamPresenter<ExamContract.IMyExam>>
        implements MyExamListAdapter.OnItemClickListener, ExamContract.IMyExam {
    private View mView;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private XRefreshView refreshView;
    private int page = 1;
    private MyExamAdapter adapter;
    private int type,status;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.recycler_view_layout, container, false);
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }


    private void initView() {
        if (getArguments() != null) {
            type = getArguments().getInt("type");
            status = getArguments().getInt("status");

        }
        refreshView = mView.findViewById(R.id.refreshView);
        refreshView.setPullLoadEnable(false);
        refreshView.setPullRefreshEnable(true);
        recyclerView = mView.findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new MyExamAdapter(R.layout.my_exam_list_item,type);
        recyclerView.setAdapter(adapter);

        initEmptyView(recyclerView);
        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                page = 1;
                loadData();
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                page++;
                loadData();
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter a, View view, int position) {
                if (view.getId() == R.id.btn1){//补考
//                    Intent intent = new Intent(mApp,ExamActivity.class);
                    Intent intent = new Intent(mApp,ExamExplainActivity.class);
                    intent.putExtra("paperId",adapter.getData().get(position).getId());
                    intent.putExtra("data",adapter.getData().get(position));
                    startActivityForResult(intent,MyExam.code);

                }else{
                    Intent intent = new Intent(getActivity(),ExamResultActivity.class);
                    intent.putExtra("data",  adapter.getData().get(position));
                    startActivity(intent);
                }

            }
        });

    }

    @Override
    public ExamPresenter createPresenter() {
        return new ExamPresenter();
    }

    @Override
    public void loadData() {
        adapter.setEmptyView(loadingView);
        Map<String, String> map = new HashMap();
        map.put("isPassed",status+"");//1：合格 0：不合格
        mPresenter.getMyExam(page, PublicResources.PageSize, map);
    }

    @Override
    public void onQrderItemClick(int position) {

    }

    @Override
    public void setMyExamList(boolean isSuccess, int page, List<ExamBean> data) {
        if (isSuccess && data != null) {

//            List<ExamBean> newData = tranList(data);
            if (page == 1) {
                adapter.setNewData(data);
                refreshView.stopRefresh();
                //显示数据大于20条才允许上拉加载更多
                if (data.size() >= PublicResources.PageSize) {
                    refreshView.setPullLoadEnable(true);
                }
                if (data.size() == 0) {
                    adapter.setEmptyView(noDataView);
                }
            } else {
                adapter.addData(data);
                refreshView.stopLoadMore();
            }
        } else {
            //如果是加载新数据时失败了 则page-1
            if (page > 1) {
                refreshView.stopLoadMore();
                this.page--;
            } else {
                //还是还原为1防止出现负数
                this.page = 1;
                refreshView.stopRefresh();
                adapter.setEmptyView(isSuccess?noDataView:errorView);
            }
        }
    }

    private List<ExamBean> tranList( List<ExamBean> data){
        List<ExamBean> newList=new ArrayList<>();
        for (ExamBean item:data){
            double v = Double.valueOf(item.getUserAvgScore()+"") - Double.valueOf(item.getPassline()+"");
            if(type==1){
                if(v>0){
                    newList.add(item);
                }
            }else{
                if(v<0){
                    newList.add(item);
                }
            }

        }
        return newList;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode!= Activity.RESULT_OK)
            return;
        switch (requestCode){
            case MyExam.code:
                refreshView.startRefresh();
                break;
        }
    }
}
